/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package htmlunit.code;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;

/**
 *
 * @author TiTa
 */
public class parse_not_page_facebook {

 
    DomElement doc;
     public  void parse_Profile_facebook(String url){
         try{
         java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
         java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
     //--!end warning
    final WebClient webClient = new WebClient();
     webClient.getOptions().setTimeout(20000);
    webClient.getOptions().setJavaScriptEnabled(false);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.waitForBackgroundJavaScript(10000);
  //ket noi trang
    final HtmlPage page=webClient.getPage(url);//loi
    
    int set_fbLongBlurb=page.getByXPath("//span[@class='fbLongBlurb']").size();
    if(set_fbLongBlurb!=0){
                      final HtmlSpan span1 = (HtmlSpan) page.getByXPath("//span[@class='fbLongBlurb']").get(0);
             // doc=div4.getElementsByTagName("div").get(0);
             String content_in_div=span1.getElementsByTagName("div").get(0).getTextContent();
            // String encode_utf8= URLDecoder.decode(content_in_div, "UTF-8");
             System.out.println(content_in_div+"\n");
             System.out.println("khong phai la trang");
    }else{
              //phuc vu cua quan
        /*
    final HtmlSpan span2 = (HtmlSpan) page.getByXPath("//span[@class='fsm fwn fcg']").get(0);
                        int so_luong=span2.getElementsByTagName("a").size();
     for(int i=0;i<so_luong;i++){
      String link1=span2.getElementsByTagName("a").get(i).getTextContent();
                       
                       
                     //  System.out.println(link1);}
                       
                     
   //  }
   
 //dia chi
      final HtmlSpan div1 = (HtmlSpan) page.getByXPath("//span[@class='fbProfileBylineLabel']").get(1);
       
       doc=div1.getElementsByTagName("span").get(0);
       String link1=doc.getElementsByTagName("a").get(0).getTextContent();
       System.out.println(link1);

        //dien thoai
       final HtmlSpan div2 = (HtmlSpan) page.getByXPath("//span[@class='fbProfileBylineLabel']").get(2);
       doc=div2.getElementsByTagName("span").get(0);
       String d=div2.getElementsByTagName("span").get(0).getTextContent();
       
       //hoat dong
        final HtmlSpan hoat_dong = (HtmlSpan) page.getByXPath("//span[@class='fbProfileBylineLabel']").get(3);
                       String link_active=hoat_dong.getTextContent();//getElementsByTagName("span").get(0).getTextContent();
                         System.out.println(link_active+"\n");
    

       System.out.println(d); */
           int  span_fbProfileBylineLabel= page.getByXPath("//span[@class='fbProfileBylineLabel']").size();
                      for(int j=0;j<span_fbProfileBylineLabel;j++){
                      final HtmlSpan span_fbProfileBylineLabel_in=(HtmlSpan) page.getByXPath("//span[@class='fbProfileBylineLabel']").get(j);
                      String  row1=span_fbProfileBylineLabel_in.getTextContent();
                      System.out.println(row1+"\n");//+row2+"\n"+row3);
                      System.out.println("khong phai ca page");
                      }
    }
      
         }  catch(Exception ex){
         
        System.out.println("loi khong truy can facebook");
         
              ///khong in ra duoc
             /*
                ket_noi_url kn=new ket_noi_url();
                HtmlPage page=kn.ketnoi(url);
                final HtmlSpan div4 = (HtmlSpan) page.getByXPath("//span[@class='fbLongBlurb']").get(0);
              doc=div4.getElementsByTagName("div").get(0);
             String content_in_div=div4.getElementsByTagName("div").get(0).getTextContent();
             System.out.println(content_in_div);
         */
         }
         
         
     }
    /* 
      public String DownloadPage(String str){
          
          
    //final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
    final WebClient webClient = new WebClient();
    webClient.getOptions().setTimeout(20000);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);

    try{
        HtmlPage page = webClient.getPage(str);
        XmlPage page2 = webClient.getPage(str);
        int n = webClient.waitForBackgroundJavaScript(100000); 

        System.out.println("Executing " + n + " JavaSript jobs!"); 
        System.out.println("OUTPUT: " + page2); 

        System.out.println("OUTPUT: " + page.asXml()); 
        webClient.closeAllWindows(); 
    }

    catch(IOException e){
        JOptionPane.showMessageDialog(null, "error");
    }


    webClient.closeAllWindows();
    return "";
}
     
     
     */
     
     public static void main(String[] args){
     
        /*  TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public void checkClientTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
        public void checkServerTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
    }
};

// Install the all-trusting trust manager
try {
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}
try {
    URL url = new URL("https://hostname/index.html");
} catch (MalformedURLException e) {
}
//SSL.
SSLContextBuilder sslctxb = new SSLContextBuilder();
        try {
            sslctxb.loadTrustMaterial(KeyStore.getInstance(KeyStore.getDefaultType()),
                                      new TrustSelfSignedStrategy() {
              @Override
              public boolean isTrusted(X509Certificate[] chain,
                                       String            authType)
                throws CertificateException {
                return true;
              }
            });
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(lay_hinh_face.lay_hinh_chua_thamso.class.getName()).log(Level.SEVERE, null, ex);
        } catch (KeyStoreException ex) {
            Logger.getLogger(lay_hinh_face.lay_hinh_chua_thamso.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            SSLContext sslctx = sslctxb.build();
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(lay_hinh_face.lay_hinh_chua_thamso.class.getName()).log(Level.SEVERE, null, ex);
        } catch (KeyManagementException ex) {
            Logger.getLogger(lay_hinh_face.lay_hinh_chua_thamso.class.getName()).log(Level.SEVERE, null, ex);
        }
        //end SSL
        * 
        * */
         
         ///////////////////////////////////////////////////////////////////////////
         parse_not_page_facebook lh=new parse_not_page_facebook();
         String url="https://www.facebook.com/Teecafevn";
             lh.parse_Profile_facebook(url);
             
             // lh.DownloadPage("https://www.facebook.com/hardrockcafevietnam");
         

  
     
}


/*truy cap tu viet nam co the duco dich ta tieng viet */
}
